package com.jxwifi.cloud.quickclean.address;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.basicactivity.BasicActivity;
import com.idroid.utils.NoDoubleClickListener;
import com.idroid.widget.LoadDialog;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.app.AccountMgr;
import com.jxwifi.cloud.quickclean.app.ClientBasicActivity;
import com.jxwifi.cloud.quickclean.app.PasswordLogin;
import com.jxwifi.cloud.quickclean.bean.CustomerAddressBean;
import com.jxwifi.cloud.quickclean.bean.PoiItemBean;
import com.jxwifi.cloud.quickclean.map.MapActivity;
import com.jxwifi.cloud.quickclean.utils.TitleClient;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;
import com.umeng.analytics.MobclickAgent;
import com.zhy.view.flowlayout.TagFlowLayout;

import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 添加地址
 * Created by Administrator on 2019/6/17.
 */

public class AddressActivity extends ClientBasicActivity implements AddressView {

    private String TAG = this.getClass().getSimpleName();
    @Bind(R.id.tv_con_order_time)
    TextView mTvConOrderTime;
    @Bind(R.id.tv_con_order_time_details)
    TextView mTvConOrderTimeDetails;
    @Bind(R.id.et_complaint_evaluation)
    EditText mEtComplaintEvaluation;

    @Bind(R.id.tv_address_label1)
    TextView mTvAddressLabel1;
    @Bind(R.id.tv_address_label2)
    TextView mTvAddressLabel2;
    @Bind(R.id.tv_address_label3)
    TextView mTvAddressLabel3;
    @Bind(R.id.tv_address_label4)
    TextView mTvAddressLabel4;
    @Bind(R.id.tv_working_confirm)
    TextView mTvWorkingConfirm;

    private LayoutInflater mInflater;

    private CustomerAddressBean mAddressBean;

    private AddressPresenter mPresenter;

    private String mIsAddOrCompile = "";//判断上个页面的操作是添加地址（add）还是编辑地址(compile)
    private int mIndexSize;

    private PoiItemBean mPoiItem;
    private LoadDialog mLoadDialog;//加载动画

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mInflater = LayoutInflater.from(this);
        setContentView(R.layout.activity_add_address);
        ButterKnife.bind(this);
        openEventBus();
        mIsAddOrCompile = getIntent().getStringExtra("addorcompile");
        if ("compile".equals(mIsAddOrCompile)) {
            mAddressBean = (CustomerAddressBean) getIntent().getExtras().getSerializable("customeraddress");
        } else {
            mIndexSize=getIntent().getIntExtra("index_size",0);
            mAddressBean = new CustomerAddressBean();
        }
        initHead();
        mPresenter = new AddressPresenter(mBaseContext, this);
        initView();
    }

    private void initHead() {
        TitleClient holder = new TitleClient(ButterKnife.findById(this, R.id.lin_client_title));
        holder.defineTitle("添加地址");
        holder.defineLeft(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                onBackPressed();
            }
        });
    }

    public void initView() {
        if ("compile".equals(mIsAddOrCompile)) {
            mPresenter.setTypeCode(mAddressBean.getTypeCode());
            mTvConOrderTimeDetails.setVisibility(View.VISIBLE);
        } else {
            mTvConOrderTimeDetails.setVisibility(View.GONE);
        }
        mPresenter.onInitView();
    }

    @OnClick({R.id.rel_con_order_time, R.id.tv_working_confirm})
    void clickCK(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.rel_con_order_time:
                intent.setClass(mBaseContext, MapActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_working_confirm:
                mPresenter.getjudge();
                MobclickAgent.onEvent(this, "tv_working_confirm");
                break;
        }
    }

    //地图中选择地址后返回的信息，添加到接口中获取数据操作
    @Subscriber(tag = "e-searchlocation")
    private void getCondItion(PoiItemBean mAskBean) {

        mAddressBean.setAddressMap(mAskBean.getSnippet());
        mAddressBean.setLat("" + mAskBean.getLatitude());
        mAddressBean.setLon("" + mAskBean.getLongitude());

        //坐标反编译获取地址信息
        ResGeoCid(mAskBean.getLatitude(), mAskBean.getLongitude());

        mPoiItem = mAskBean;
        mTvConOrderTime.setText(mAskBean.getTitle());
        mTvConOrderTimeDetails.setVisibility(View.VISIBLE);
        mTvConOrderTimeDetails.setText(mAskBean.getSnippet());
    }

    @Override
    public void LoadDialogShow() {
        mLoadDialog = new LoadDialog(mBaseContext, "");
        mLoadDialog.show();
        mTvWorkingConfirm.setClickable(false);
    }

    @Override
    public void LoadDialogdismiss() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mLoadDialog.dismiss();
                mTvWorkingConfirm.setClickable(true);
            }
        }, 1000);
    }

    @Override
    public TextView getConOrderTimeDetails() {
        return mTvConOrderTimeDetails;
    }

    @Override
    public TextView getConOrderTime() {
        return mTvConOrderTime;
    }

    public String getIsAddOrCompile() {
        return mIsAddOrCompile;
    }

    @Override
    public PoiItemBean getPoiItem() {
        return mPoiItem;
    }

    @Override
    public EditText getEdContent() {
        return mEtComplaintEvaluation;
    }

    @Override
    public TextView getLabel1() {
        return mTvAddressLabel1;
    }

    @Override
    public int getIndexSize() {
        return mIndexSize;
    }

    @Override
    public TextView getLabel2() {
        return mTvAddressLabel2;
    }

    @Override
    public TextView getLabel3() {
        return mTvAddressLabel3;
    }

    @Override
    public TextView getLabel4() {
        return mTvAddressLabel4;
    }

    @Override
    public LayoutInflater getInflater() {
        return mInflater;
    }

    @Override
    public CustomerAddressBean getAddressBean() {
        return mAddressBean;
    }

    @Override
    public void onFailure(int errorCode, String msg) {
        Toast toast = Toast.makeText(mBaseContext, msg, Toast.LENGTH_SHORT);
        ToastUtils.showMyToast(toast, 3000);
    }

    @Override
    public void onSuccess() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mLoadDialog.dismiss();
                mTvWorkingConfirm.setClickable(true);
                finish();
            }
        }, 500);
    }

    @Override
    public void onCompile() {
        PasswordLogin.passwordlogin(mBaseContext, TAG, AccountMgr.gettell(mBaseContext), AccountMgr.getpassword(mBaseContext));
        if (PasswordLogin.mIsLogin) {
            mPresenter.getCompile();
        }
    }

    public void ResGeoCid(double lat, double lon) {
        GeocodeSearch geocoderSearch = new GeocodeSearch(this);
        LatLonPoint mPoint = new LatLonPoint(lat, lon);
        RegeocodeQuery query = new RegeocodeQuery(mPoint, 200, GeocodeSearch.AMAP);
        geocoderSearch.getFromLocationAsyn(query);
        geocoderSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
            @Override
            public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {

                if (!TextUtils.isEmpty(regeocodeResult.getRegeocodeAddress().getDistrict())){
                    mAddressBean.setArea(regeocodeResult.getRegeocodeAddress().getDistrict());
                    Log.i(TAG,"getCompile ==>getDistrict getArea = "+regeocodeResult.getRegeocodeAddress().getDistrict());
                }else{
                    mAddressBean.setArea(regeocodeResult.getRegeocodeAddress().getTownship());
                    Log.i(TAG,"getCompile ==>getTownship getArea = "+regeocodeResult.getRegeocodeAddress().getTownship());
                }
                mAddressBean.setCity(regeocodeResult.getRegeocodeAddress().getCity());
                Log.i(TAG,"ResGeoCid ==> getCity = "+regeocodeResult.getRegeocodeAddress().getCity());
                mAddressBean.setProvince(regeocodeResult.getRegeocodeAddress().getProvince());
            }

            @Override
            public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {

            }
        });
    }
}
